package org.tinygroup.channel.protocol.impl;

import org.tinygroup.channel.protocol.ProtocolProcess;
import org.tinygroup.context.Context;

@SuppressWarnings({ "rawtypes", "unchecked" })
public class BaseProtocolIn<ProRequest, ProResponse> extends
		AbstractProtocolIn<ProRequest, ProResponse> {
	private ProtocolProcess protocolProcess;

	public void setProtocolProcess(ProtocolProcess protocolProcess) {
		this.protocolProcess = protocolProcess;
	}

	public ProtocolProcess getProtocolProcess() {
		return protocolProcess;
	}

	protected ProResponse realDeal(ProRequest proRequest, Context context) {
		// 处理协议对象
		proRequest = afterReceived(proRequest, context);
		// 协议对象转业务对象
		Object bizRequest = getProtocolTrans().transProReq2BizReq(proRequest,
				context);
		// 处理业务对象
		bizRequest = afterRequestToBizTrans(proRequest, bizRequest, context);
		// 执行业务逻辑
		Object bizResponse = getProtocolProcess().process(bizRequest, context);
		// 处理业务响应对象
		bizResponse = afterProcess(proRequest, bizResponse, context);
		// 业务响应对象转协议对象
		ProResponse proResponse = (ProResponse) getProtocolTrans()
				.transBizRes2ProRes(bizResponse, context);
		// 处理协议对象
		proResponse = afterBizToResponseTrans(bizResponse, proResponse, context);
		return proResponse;
	}

}
